package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.enumeration.OperationType;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        List<Dish> dishList = dishMapper.page(dishPageQueryDTO);
        Page<Dish> page = (Page<Dish>) dishList;
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional
    @AutoFill(value = OperationType.INSERT)
    public void add(Dish dish, List<DishFlavor> flavors) {
        dish.setStatus(StatusConstant.ENABLE);
        dishMapper.add(dish);

        if (flavors != null){
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dish.getId()));
            dishFlavorMapper.add(flavors);
        }
    }

    @Override
    public void update(Dish dish, List<DishFlavor> flavors) {
        dishMapper.update(dish);
        dishFlavorMapper.deleteByDishId(dish.getId());
        if (flavors != null && flavors.size() > 0){
            flavors.forEach(dishFlavor ->
                    dishFlavor.setDishId(dish.getId()));
            dishFlavorMapper.add(flavors);
        }
    }

    @Override
    public DishVO getById(Long id) {
        List<DishFlavor> dishFlavors = dishFlavorMapper.getById(id);
        Dish dish = dishMapper.getById(id);
        DishVO dishVO = DishVO.builder()
                .id(dish.getId())
                .name(dish.getName())
                .categoryId(dish.getCategoryId())
                .price(dish.getPrice())
                .image(dish.getImage())
                .description(dish.getDescription())
                .status(dish.getStatus())
                .updateTime(dish.getUpdateTime())
                .flavors(dishFlavors).build();
        return dishVO;

    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void delete(Long[] ids) {
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if(dish.getStatus() == StatusConstant.ENABLE)
                throw new DeletionNotAllowedException("当前菜品不允许删除");
            Long[] mealids = setMealDishMapper.getSetMealIdByDishId(id);
            if (mealids != null && mealids.length > 0)
                throw new DeletionNotAllowedException("当前菜品正被套餐使用，不允许删除");
            dishFlavorMapper.deleteByDishId(id);
        }
        dishMapper.delete(ids);

    }

    @Override
    public void status(Integer status, Long id) {
        dishMapper.status(status,id);
    }

    @Override
    public List<Dish> list(Long categoryId) {
        return dishMapper.list(categoryId);
    }
    @Override

    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish.getCategoryId());

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getById(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
